//Iditarod Trail
//Amelia Bauer, Elise Eiden, Christine Gerardi, Taryn Green
//CSE 20212 2012
/*Player to represent the user and their data members*/

#include <iostream>
#include <cstdio>
#include <string>
#include "Player.h"
#include "Dog.h"
#include "DeluxeDog.h"
#include <stdlib.h>
using namespace std;



/*default constructor*/
Player::Player() {
	location = 0;		//Starting point 
	name = "Bob Bobsled";
	gender = true;
	numDog = 3;
	foodLevel = 100;
	medicineLevel = 0;
	money = 1000;
	for(int i=0; i<8; i++)
		dogs[i] = new Dog();
	//Kill off extra dogs
	//From dogs 3-8, keep 3 alive
	for(int i = 3; i<8; i++)			
		dogs[i]->kill();
	days = 0;
}
/*non-default*/
Player::Player(string name_in, bool gender_in){
	location = 0;
	name = name_in;
	gender = gender_in;
	numDog = 3;
	days = 0;
}
//Get & set functions 
int Player:: getLocation()
{
	return location;
}
int Player:: getMoney()
{
	return money;
}
void Player:: setMoney(int a)
{
	money = a;	
}
int Player:: getFood()
{
	return foodLevel;
}
int Player:: getNumSick()
{
	return numSick;
}
int Player:: getDays()
{
	return days;
}
void Player::addFood(int a)
{
	foodLevel += a;
}
void Player:: addMedicine(int a)
{
	medicineLevel += a*10;
}
int Player:: getNumDog()		
{
	return numDog;
}
void Player:: setNameDogs(int index, string name)
{
	dogs[index]->setName(name);
}
// set name
void Player::setName(string name_in){
	name = name_in;
}

// get name
string Player::getName() {
	return name;
}

// set gender
void Player::setGender(bool gender_in) {
	gender = gender_in;
}

// get gender
bool Player::getGender() { 
	return gender;
}

string Player:: getDogName(int a)
{
	return dogs[a]->getName();
}
int Player:: getNumAlive(){
	int alive=0;
	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive()) 
			alive++;
	}
	return alive;
}
//Temporary for debugging!
void Player:: displayDogNames()
{
	cout << endl << "Number of dogs: " << numDog << endl;
	for(int i = 0; i<numDog; i++)
		cout << (i+1) << " " << dogs[i]->getName() << "\t" << dogs[i]->getStrength() << endl;
	cout << "Medicine " << medicineLevel << endl;
	cout << "Food " << foodLevel << endl;
	cout << "Your name: " << name << endl;
	cout << "Your gender: ";
	if(gender)
		cout << "female" << endl;
	else
		cout << "male" << endl;
}
//Initializes newly bought dogs
void Player:: buyNewDogs(int num, int numDeluxe)
{
	for(int i = numDog; i<(numDog+num); i++)
		dogs[i]->makeAlive();
	for(int i = numDog+num; i<numDog+num+numDeluxe; i++)
		dogs[i] = new DeluxeDog();
	numDog+= num + numDeluxe;
}
//Uses medicine & increases health or returns error
int Player:: useMedicine()
{
	if(medicineLevel >=5)
		medicineLevel -= 5;		//Arbitrary number to use
	else
	{
		return 1;	//Returns 1 such that an error message is printed in the window
	}
	int index = 0;
	while(dogs[index]->getHealth()==-1)
		index++;
	for(int i =0; i<numDog; i++)	//Finds least healthy dog to use medicine on 
	{
		if(dogs[i]->getHealth()<dogs[index]->getHealth() && dogs[i]->getHealth()!=-1)
			index = i;
	}
	dogs[index]->useMedicineDog();
return 0;
}
void Player::starveDog()      //if there is not enough food to feed all dogs, decrease health of least healthy dog
{ 
  int count=0;
  while(dogs[count]->getHealth()==-1)
		count++;
	for(int i =0; i<numDog; i++)	//Finds least healthy dog 
	{
		if(dogs[i]->getHealth()<dogs[count]->getHealth() && dogs[i]->getHealth()!=-1)
			count = i;
  }      
     dogs[count]->starve();
}
//Calculates the speed of the player 
float Player:: getSpeed()
{
	float speed=0;
	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive())
			speed += dogs[i]->calcSpeed();
	}
	if(getNumAlive()==0)
		return 0;
	speed /= (float) getNumAlive();				//Average speed
	if(speed > 6)
		return 6;
	speed /= 2.0;
if(speed < 1)
	return 1;
return speed;
}
//Actual playing of game, advances time, eats food, deals with health, etc. 
void Player:: play()
{
	float speed = getSpeed();
	//Prepare for next day
	location += (int) speed;
	for(int i=0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive()){
			dogs[i]->timeAdvance();	//Decreases health of dog randomly 
		}
	}
	if(foodLevel<getNumAlive())
	{
		starveDog();
		foodLevel = 0;
	}
	else
		foodLevel -= getNumAlive();	//subtract one unit of food per dog, per day

	days++;
}
//Calls rest on each dog for the correct number of days 
void Player:: restForDays(int daysR)
{
	for(int i = 0; i<daysR; i++)
	{
		for(int j = 0; j<numDog; j++)
			dogs[i]->rest();

		if(foodLevel<getNumAlive())
		{
			starveDog();
			foodLevel = 0;
		}
		else
			foodLevel -= getNumAlive();	//subtract one unit of food per dog, per day
	}
	days += daysR;
	
}
//Decides if a dog is dying 
int Player:: checkIfSick()
{
	numSick = 0;
	int dead = 10;

	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->getHealth()==0)
		{
			dead = i;
		}
		if(dogs[i]->getHealth()<=3 && dogs[i]->checkAlive())
		{
			numSick ++;
		}
	}
	if(dead <8)
		dogs[dead]->kill();
	return dead;
}
//Finds out if the game is done based on location or number of dogs 
int Player:: gameDone()
{
	if(location < 0)
	{
		return 1;
	}
	else if(location >= 180)
	{
		return 2;
	}
	else if(checkAllDogsDead()>0)
	{
		return 3;
	}
	return 0;
}
//Note: Could simply use getNumAlive() and check for equal to 0, but this function was made first and getNumAlive() was necessary for other things 
int Player:: checkAllDogsDead()			//returns 1 if all dogs are dead
{	
	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive()) 
			return 0;
	}
	return 1;
}
//Returns names of dogs that can hunt in one string 
string Player:: huntPlayerNames()
{
	string names = "";
	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive() && dogs[i]->getStrength() == 10)
		{
			if(names == "")
				names = dogs[i]->getName();
			else
				names += " & " + dogs[i]->getName();
		}
	}
	return names;
}
//Returns how much food was gathered
int Player:: huntPlayerFood()
{
	int f = 0;
	int temp;
	for(int i = 0; i<numDog; i++)
	{
		if(dogs[i]->checkAlive() && dogs[i]->getStrength() == 10)
		{
			temp = rand()%50;
			f += temp;
		}
	}
	foodLevel += f;
	return f;
	
}
//Kills least healthy dog 
string Player:: palinAttack()
{
	  int count=0;
	  while(dogs[count]->getHealth()==-1)
		count++;
	for(int i =0; i<numDog; i++)	//Finds least healthy dog 
	{
		if(dogs[i]->getHealth()<dogs[count]->getHealth() && dogs[i]->getHealth()!=-1)
			count = i;
	}      
	string nameDog = dogs[count]->getName();
     dogs[count]->kill();
return nameDog;
}
//Takes random amount of food
void Player:: yetiAttack()
{
	int foodTake;
	if(foodLevel > 0)
	{
		foodTake = rand()%foodLevel;
		foodLevel -= foodTake;
	}
	else	//Takes nothing if nothing to take 
		return;

}
